package Olivia.olivia3d
{
	import Olivia.olivia3d.camera.OCamera;
	import Olivia.olivia3d.model.ms3d.Ms3dManager;
	import Olivia.olivia3d.model.ms3d.Ms3dStruct;
	import Olivia.olivia3d.model.ms3d.TempSimplifyModel;
	import Olivia.olivia3d.render.RenderManager;
	import Olivia.olivia3d.vo.Ms3dVo;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.EventDispatcher;
	
	public class Olivia3D extends EventDispatcher
	{
		private static var _self:Olivia3D;
		public static function get ins():Olivia3D
		{
			if(!_self){
				_self = new Olivia3D();
			}
			return _self;
		}
		
		public var main:Sprite;
		public var width:int = 800;
		public var height:int = 600;
		public var mainCamera:OCamera;
		//model
		public var midx:uint = 0;
		public var models:Array = [];
		
		public function Olivia3D()
		{
			
		}
		
		public function init(spr:Sprite):void
		{
			//init main sprite			
			main = spr;
			main.stage.scaleMode = StageScaleMode.NO_SCALE;
			main.stage.align = StageAlign.TOP_LEFT;
			
			//create maincamera
			mainCamera = new OCamera(main.stage);
			mainCamera.viewEye.x = 20;
			mainCamera.viewEye.y = 260;
			mainCamera.viewEye.z = -50;

			//init render Manager
			RenderManager.ins.init(mainCamera);
		}
		
		public function addModel(file:String,msvo:Ms3dVo,stgIdx:int=0):void
		{
			if(stgIdx < 0 || stgIdx > 3)
				return;
			models[midx] = msvo;
			msvo.msid = midx++;
			Ms3dManager.load(file,dataComplete,[msvo,stgIdx]);
		}
		
		private function dataComplete(ms3d:Ms3dStruct,arr:Array):void
		{
			var msvo:Ms3dVo = arr[0];
			var stgIdx:int = arr[1];
			
			TempSimplifyModel.reusemodel(msvo,ms3d);
			RenderManager.ins.addModelInRender(stgIdx,msvo);
		}
	}
}
